import request from '../../utils/request'

export default {
  namespaced: true,
  state: {
    /* orderList: [], // 订单列表
    totalNum: 0, // 总条数
    totalPage: 0, // 总页数 */
    confirmOrderList: [],
    checkInfoList: {},

    loding: false,
    orderList: [],
    evaluateList: [], // 待评价列表
    notList: false,   // 您还没有相关订单
    finished: false,  // 数据是否加载完(下拉刷新组件)
  },
  mutations: {
    /* SET_ORDER_LIST(state, content) {
      state.orderList = [...state.orderList, ...content]
    },
    SET_TOTALNUM(state, content) {
      state.totalNum = content
    },
    SET_ORDER_LIST_INIT(state) {
      state.orderList = []
    },*/
    SET_CONFIRM_ORDER_LIST(state, content) {
      state.confirmOrderList = content
    },
    SET_CHECK_INFO_LIST(state, content) {
      state.checkInfoList = content
    }
  },
  actions: {
    /******** 获取订单列表 ********/
    getOrderList({ commit, state }, content ) {
      let {active, eventName, pageNum} = content
      let url = '/order/getOrderList'
      let params = {
        pageNum,
        pageSize: 10
      }

      // 获取active   0=>全部 1=>待付款  2=>待发货 3=>待收货  4=>待评价
      // 传给后台     默认    0->待付款；1->待发货；2->已发货；3->已收货；4->已关闭；5->已完成
      switch(active) {
        case 1:
          params.status = 0
          break;
        case 2:
          params.status = 1
          break;
        case 3:
          params.status = 2
          break;
        case 4:
          // 待评价调另外一个接口
          url = '/orderItem/unCommentItems'
          break;
      }

      state.loding = true

      return new Promise((resolve, reject) => {
        request.post(url, params)
          .then(data => {
            /* 
              操作列表
                1.下拉刷新 => 只加载第一页, 赋值替换
                2.上拉加载 => push 追加
                3.点击切换 => 只加载第一页, 赋值替换, + 滚动到顶部
            */

            if (eventName == 'pull-down' || eventName == 'click') {

              // 待评价列表数据结构和其它不同，为了体验使用另一个数据
              active != 4? state.orderList = data.info : state.evaluateList = data.info
  
              state.finished = false
  
            } else if (eventName == 'pull-up') {
              
              let arr = data.info
  
              if (active != 4) {
                for (let i in arr) {
                  state.orderList.push(arr[i])
                }
              } else {
                for (let i in arr) {
                  state.evaluateList.push(arr[i])
                }
              }
   
            }
            // 数据全部加载完成
            if (data.page >= data.totalpage) {
              state.finished = true
            }

            // 您还没有相关订单
            if (active != 4 ) {
              state.orderList.length == 0? state.notList = true : state.notList = false
            } else {
              state.evaluateList.length == 0? state.notList = true : state.notList = false
            }
            resolve(data)
          })
          .catch(error => {
            reject(error)
          })
      })
    }

    // 获取订单列表
    /* getOrderList({ commit }, data) {
      return new Promise((resolve, reject) => {
        request
          .post('/order/getOrderList', data)
          .then(data => {
            commit('SET_ORDER_LIST', data.info)
            commit('SET_TOTALNUM', data.totalnum)
            resolve()
          })
          .catch(error => {
            reject(error)
          })
      })
    } */
  }
}
